//    Dhammapada PSP
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include <pspkernel.h>
#include <pspsdk.h>
#include <stdio.h>
#include <oslib/oslib.h>

#include "viewcategory.h"
#include "dhammapada.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int viewCatRetValue = 0;
static int exitFlagViewCat = 0;
char buffer[262];
int scrollbarHeight = 0;
int scrollbarDelta = 0;
struct settings *lUserSettings;
OSL_FONT *fontText;
OSL_IMAGE *bkgViewCat;
OSL_IMAGE *bkgTranslated;
OSL_IMAGE *bkgOriginal;
OSL_IMAGE *toolbar;
OSL_IMAGE *bkgScrollbar;
OSL_IMAGE *scrollbar;
OSL_IMAGE *helpViewCat;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Back to index:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int backToIndex(){
    viewCatRetValue = lUserSettings->previousMode;
    exitFlagViewCat = 1;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Draw a section:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void drawSection(struct settings *userSettings){
    int row = 0;
    int xPos = 0;
    int yPos = 0;
    
    struct section sec = currentCategory.sections[userSettings->lastSection];
    oslSetFont(fontText);
    oslSetBkColor(RGBA(0, 0, 0, 0));
    
    //Toolbar:
    oslSetTextColor(RGBA(0, 0, 0, 255));
    oslDrawImageXY(toolbar, 0, 0);
    oslDrawImageXY(toolbar, 0, 272 - toolbar->sizeY);
    snprintf(buffer, sizeof(buffer), "%i. %s", currentCategory.index + 1, currentCategory.title);
    oslDrawString(2, 1, buffer);
    snprintf(buffer, sizeof(buffer), "%i", sec.index);
    oslDrawString(450, 1, buffer);
    snprintf(buffer, sizeof(buffer), "%i. %s", currentCategoryOriginal.index + 1, currentCategoryOriginal.title);
    oslDrawString(2, 270 - fontText->charHeight, buffer);

    //Scrollbar:
    oslDrawImageXY(bkgScrollbar, 1, 22);
    if (userSettings->lastSection < scrollbarDelta){
        scrollbar->stretchY = scrollbarHeight + 1;
        oslDrawImageXY(scrollbar, 2, 22 + userSettings->lastSection * (scrollbarHeight + 1));
    }else{
        scrollbar->stretchY = scrollbarHeight;
        oslDrawImageXY(scrollbar, 2, 22 + userSettings->lastSection * scrollbarHeight + scrollbarDelta);
    }
    
    //Translated [from y=22 to y=170]:
    oslSetTextColor(RGBA(255, 255, 255, 255));
    oslDrawImageXY(bkgTranslated, 7, 22);
    int startY = 22 + (170 - 22 - fontText->charHeight * sec.rowsNumber) / 2.0;
    for (row=0; row<sec.rowsNumber; row++){
        yPos = startY + row * fontText->charHeight;
        xPos = 480 / 2 - oslGetStringWidth(sec.rows[row]) / 2.0;
        oslDrawString(xPos, yPos, sec.rows[row]);
    }
    
    //Original Pali [from y=175 to y=250]:
    oslDrawImageXY(bkgOriginal, 7, 175);
    sec = currentCategoryOriginal.sections[userSettings->lastSection];
    startY = 175 + (250 - 175 - fontText->charHeight * sec.rowsNumber) / 2.0;
    for (row=0; row<sec.rowsNumber; row++){
        yPos = startY + row * fontText->charHeight;
        xPos = 480 / 2 - oslGetStringWidth(sec.rows[row]) / 2.0;
        oslDrawString(xPos, yPos, sec.rows[row]);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loads the background image:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loadBackground(struct settings *userSettings, int deleteBkg){
    if (bkgViewCat && deleteBkg)
        oslDeleteImage(bkgViewCat);

    sprintf(buffer, "%s/images/bkg_%2.2i.png", userSettings->ebootPath, userSettings->lastID + 1);
    bkgViewCat = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkgViewCat){
        sprintf(buffer, "%s/images/bkg_00.png", userSettings->ebootPath);
        bkgViewCat = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
        if (!bkgViewCat)
            errorLoadImage(buffer);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Update category into memory:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateCategory(struct settings *userSettings, int deleteBkg){
    loadCategory(userSettings->lastID);
    loadBackground(userSettings, deleteBkg);
    //Prepare scrollbar:
    scrollbarHeight = floor((float)bkgScrollbar->sizeY / (float)currentCategory.sectionsNumber);
    scrollbarDelta = bkgScrollbar->sizeY - scrollbarHeight * currentCategory.sectionsNumber;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Next category:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void nextCategory(struct settings *userSettings){
    if (userSettings->lastID < categoriesIndexOriginal.count - 1){
        userSettings->lastID++;
        userSettings->lastSection = 0;
        updateCategory(userSettings, 1);
    }else{
        userSettings->lastID = 0;
        userSettings->lastSection = 0;
        updateCategory(userSettings, 1);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Previous category:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void previousCategory(struct settings *userSettings){
    if (userSettings->lastID > 0){
        userSettings->lastID--;
        userSettings->lastSection = 0;
        updateCategory(userSettings, 1);
    }else{
        userSettings->lastID = categoriesIndexOriginal.count - 1;
        userSettings->lastSection = 0;
        updateCategory(userSettings, 1);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Show help:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void showHelpViewCategory(){
    while(!osl_quit && !exitFlagViewCat){
        oslStartDrawing();
        oslDrawImageXY(helpViewCat, 0, 0);
        oslReadKeys();
        if (osl_keys->pressed.start)
            break;
    	oslEndDrawing();
    	oslSyncFrame();
    	oslAudioVSync();
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// View a category:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int viewCategory(struct settings *userSettings){
    viewCatRetValue = -1;
    lUserSettings = userSettings;
    
    //Load images:
    sprintf(buffer, "%s/images/toolbar.png", userSettings->ebootPath);
    toolbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!toolbar)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/bkgoriginal.png", userSettings->ebootPath);
    bkgOriginal = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkgOriginal)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/bkgtranslated.png", userSettings->ebootPath);
    bkgTranslated = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkgTranslated)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/fontText.oft", userSettings->ebootPath);
    fontText = oslLoadFontFile(buffer);
    if (!fontText)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/bkgscrollbar.png", userSettings->ebootPath);
    bkgScrollbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkgScrollbar)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/scrollbar.png", userSettings->ebootPath);
    scrollbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!scrollbar)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/pointer.png", userSettings->ebootPath);
    OSL_IMAGE *anim = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!anim)
        errorLoadImage(buffer);
    oslSetImageRotCenter(anim);

    /*sprintf(buffer, "%s/images/helpviewcategory.png", userSettings->ebootPath);
    helpViewCat = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!helpViewCat)
        errorLoadImage(buffer);*/

    //Load selected category:
    updateCategory(userSettings, 0);
    
    exitFlagViewCat = 0;
    while(!osl_quit && !exitFlagViewCat){
        oslStartDrawing();
        oslDrawImageXY(bkgViewCat, 0, 0);
        drawSection(userSettings);
        oslDrawImageXY(anim, 480 - anim->centerX - 2, 272 - anim->centerY);
        if (++anim->angle == 360)
            anim->angle = 0;

        oslReadKeys();
        if (osl_keys->pressed.down || osl_keys->pressed.cross){
            if (userSettings->lastSection < currentCategory.sectionsNumber - 1)
                userSettings->lastSection++;
            else
                nextCategory(userSettings);
        }else if (osl_keys->pressed.up || osl_keys->pressed.triangle){
            if (userSettings->lastSection > 0)
                userSettings->lastSection--;
            else
                previousCategory(userSettings);
        }else if (osl_keys->pressed.right || osl_keys->pressed.R){
            nextCategory(userSettings);
        }else if (osl_keys->pressed.left || osl_keys->pressed.L){
            previousCategory(userSettings);
        }else if (osl_keys->pressed.circle){
            backToIndex();
        /*}else if (osl_keys->pressed.start){
            showHelpViewCategory();*/
        }

    	oslEndDrawing();
    	oslSyncFrame();
    	oslAudioVSync();
    }
    //unLoad images:
    oslDeleteImage(bkgViewCat);
    oslDeleteImage(toolbar);
    oslDeleteImage(bkgTranslated);
    oslDeleteImage(anim);
    oslDeleteImage(bkgOriginal);
    oslDeleteImage(bkgScrollbar);
    oslDeleteImage(scrollbar);
    oslDeleteFont(fontText);
    //oslDeleteImage(helpViewCat);
    return viewCatRetValue;
}

